package com.deyuanyun.pic.pbf.controller.dto.defect;

import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.pbf.enumeration.ImgCustomType;
import com.deyuanyun.pic.settings.domain.base.Attachment;
import com.deyuanyun.pic.settings.support.biz.AttachmentUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图片转换
 *
 * @author huanglw
 * @date 2016-7-8
 *
 * History: [修改人][时间][修改描述]
 */
public class ImgDto extends DefaultCommand<Attachment>{

    /**
     * 主键编号
     */
    private String id;

    /**
     * 备注
     */
    private String remark;

    /**
     * 文件名称
     */
    private String realName;

    /**
     * 文件保存路径
     */
    private String path;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Override
    public void fromDomain(Attachment domain) {

        if (ObjectUtil.isEmptyAll(domain)) {
            return;
        }

        this.id = domain.getId();
        this.remark = domain.getCreateRemark();
        this.realName = domain.getRealName();
        this.path = domain.getPath();
    }

    public static Map<String, List<ImgDto>> queryImgDtoMapList(String businessId) {
        Map<String, List<ImgDto>> map = new HashMap();
        map.put(ImgCustomType.ALL.toString(), queryImgDtoList(businessId));
        return map;
    }

    public static List<ImgDto> queryImgDtoList(String businessId) {
        List<Attachment> attachmentList = AttachmentUtil.getAttachmentList(businessId);
        List<ImgDto> imgDtoList = new ArrayList<ImgDto>();
        for (Attachment attachment : attachmentList) {
            ImgDto imgDto = new ImgDto();
            imgDto.fromDomain(attachment);
            imgDtoList.add(imgDto);
        }
        return imgDtoList;
    }

    /**
     * 分类查询图片组装
     *
     * @param businessId 业务编码
     * @param imgCustomTypes 自定义图片类型
     * @return
     */
    public static Map<String, List<ImgDto>> queryClassifyImgDto(String businessId, ImgCustomType[] imgCustomTypes) {

        List<Attachment> attachmentList = AttachmentUtil.getAttachmentList(businessId);
        List<ImgDto> repairBeforeImgList = new ArrayList<ImgDto>();
        List<ImgDto> repairDuringList = new ArrayList<ImgDto>();
        List<ImgDto> repairAfterImgList = new ArrayList<ImgDto>();
        List<ImgDto> ndttypeImgList = new ArrayList<ImgDto>();
        List<ImgDto> weldworktypeImgList = new ArrayList<ImgDto>();
        List<ImgDto> allImgDtos = new ArrayList<ImgDto>();
        for (Attachment attachment : attachmentList) {
            ImgDto imgDto = new ImgDto();
            imgDto.fromDomain(attachment);
            if (ImgCustomType.REPAIR_BEFORE.toString().equals(attachment.getCustomType())) {
                repairBeforeImgList.add(imgDto);
            } else if (ImgCustomType.REPAIR_DURING.toString().equals(attachment.getCustomType())) {
                repairDuringList.add(imgDto);
            } else if (ImgCustomType.REPAIR_AFTER.toString().equals(attachment.getCustomType())) {
                repairAfterImgList.add(imgDto);
            } else if (ImgCustomType.NDTTYPE.toString().equals(attachment.getCustomType())) {
                ndttypeImgList.add(imgDto);
            } else if (ImgCustomType.WELDWORKTYPE.toString().equals(attachment.getCustomType())) {
                weldworktypeImgList.add(imgDto);
            } else {
                allImgDtos.add(imgDto);
            }
        }

        Map<String, List<ImgDto>> map = new HashMap();
        for (ImgCustomType imgCustomType : imgCustomTypes) {
            if (imgCustomType.REPAIR_BEFORE.equals(imgCustomType)){
                map.put(ImgCustomType.REPAIR_BEFORE.toString(), repairBeforeImgList);
            } else if (imgCustomType.REPAIR_DURING.equals(imgCustomType)){
                map.put(ImgCustomType.REPAIR_DURING.toString(), repairDuringList);
            } else if (imgCustomType.REPAIR_AFTER.equals(imgCustomType)){
                map.put(ImgCustomType.REPAIR_AFTER.toString(), repairAfterImgList);
            } else if (imgCustomType.NDTTYPE.equals(imgCustomType)){
                map.put(ImgCustomType.NDTTYPE.toString(), ndttypeImgList);
            } else if (imgCustomType.WELDWORKTYPE.equals(imgCustomType)){
                map.put(ImgCustomType.WELDWORKTYPE.toString(), weldworktypeImgList);
            } else {
                map.put(ImgCustomType.ALL.toString(), allImgDtos);
            }
        }

        return map;
    }
}
